package main.leetcode.primary.from001to100;

/**
 * 63.不同路径II
 *
 * <p>一个机器人位于一个 m x n 网格的左上角 （起始点在下图中标记为“Start” ）。
 *
 * <p>机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角（在下图中标记为“Finish”）。
 *
 * <p>现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径？
 *
 * <p>网格中的障碍物和空位置分别用 1 和 0 来表示。
 *
 * <p>说明：m 和 n 的值均不超过 100。
 *
 * <p>示例 1: 输入: [   [0,0,0],   [0,1,0],   [0,0,0] ] 输出: 2 解释: 3x3 网格的正中间有一个障碍物。 从左上角到右下角一共有 2
 * 条不同的路径： 1. 向右 -> 向右 -> 向下 -> 向下 2. 向下 -> 向下 -> 向右 -> 向右
 *
 * <p>来源：力扣（LeetCode） 链接：https://leetcode-cn.com/problems/unique-paths-ii
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 */
public class ex63 {
    public static void main(String[] args) {
        System.out.println(
                new ex63().uniquePathsWithObstacles(new int[][] {{0, 0, 0}, {0, 1, 0}, {0, 0, 0}}));
        System.out.println(new ex63().uniquePathsWithObstacles(new int[][] {{0}, {0}}));
        //        System.out.println(new ex63().uniquePathsWithObstacles(new int[][] {{1}}));
        System.out.println(
                new ex63()
                        .uniquePathsWithObstacles(
                                new int[][] {
                                    {
                                        0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
                                        0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0
                                    },
                                    {
                                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0,
                                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
                                    },
                                    {
                                        0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                        0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0
                                    },
                                    {
                                        1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1,
                                        1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1
                                    },
                                    {
                                        0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                        0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0
                                    },
                                    {
                                        0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
                                        1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0
                                    },
                                    {
                                        1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
                                        0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0
                                    },
                                    {
                                        0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0,
                                        1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0
                                    },
                                    {
                                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                        0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0
                                    },
                                    {
                                        0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                        0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0
                                    },
                                    {
                                        0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
                                        1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
                                    },
                                    {
                                        1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1,
                                        0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1
                                    },
                                    {
                                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1,
                                        0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0
                                    },
                                    {
                                        0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1,
                                        1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0
                                    },
                                    {
                                        0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0,
                                        0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1
                                    },
                                    {
                                        1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
                                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
                                    },
                                    {
                                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0,
                                        0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0
                                    },
                                    {
                                        0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0,
                                        0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1
                                    },
                                    {
                                        0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
                                        0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1
                                    },
                                    {
                                        1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0,
                                        1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
                                    },
                                    {
                                        0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
                                        0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1
                                    },
                                    {
                                        0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
                                        0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0
                                    }
                                }));
    }

    //    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
    //        int m = obstacleGrid.length;
    //        int n = obstacleGrid[0].length;
    //        if (obstacleGrid[0][0] == 1 || obstacleGrid[m - 1][n - 1] == 1) {
    //            return 0;
    //        }
    //        int[][] dp = new int[m][n];
    //        dp[0][0] = 1;
    //        for (int i = 1; i < m; ++i) {
    //            if (obstacleGrid[i][0] == 0) {
    //                dp[i][0] = dp[i - 1][0];
    //            }
    //        }
    //        for (int i = 1; i < n; ++i) {
    //            if (obstacleGrid[0][i] == 0) {
    //                dp[0][i] = dp[0][i - 1];
    //            }
    //        }
    //        for (int i = 1; i < m; ++i) {
    //            for (int j = 1; j < n; ++j) {
    //                if (obstacleGrid[i][j] == 0) {
    //                    dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
    //                }
    //            }
    //        }
    //        return dp[m - 1][n - 1];
    //    }

    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;

        // 起点/终点有障碍物
        if (obstacleGrid[0][0] == 1 || obstacleGrid[m - 1][n - 1] == 1) {
            return 0;
        }

        int[] dp = new int[n];

        // 初始化第一行
        for (int i = 0; i < n; ++i) {
            // 无障碍物为1
            if (obstacleGrid[0][i] == 0) {
                dp[i] = 1;
            } else { // 有障碍物直接退出，说明后面都不可达
                break;
            }
        }

        // 滚动行
        for (int i = 1; i < m; ++i) {
            // 如果行首有障碍物，重新置0
            if (obstacleGrid[i][0] == 1) {
                dp[0] = 0;
            }
            for (int j = 1; j < n; ++j) {
                // 遇到障碍物，重新置0，说明此格不可达
                if (obstacleGrid[i][j] == 1) {
                    dp[j] = 0;
                } else { // 状态改变，由于dp[j]本身已经记录了来自上方的路径数，因此只用加上来自左边的路径数即可
                    dp[j] += dp[j - 1];
                }
            }
        }
        return dp[n - 1];
    }
}
